import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';
import '../button/kq_ink_well.dart';
import '../divider/kq_divider.dart';

/// 自定义弹窗
class KqCustomDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 宽度，默认320
  final double? width;

  final Color? bgColor;

  final Widget Function() customWidgetBuilder;

  /// 按钮的自定义布局
  final Widget? customBtnWidget;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function()? onLeftBtnTap;

  /// 右边按钮回调
  final Function()? onRightBtnTap;

  /// 标题文字颜色
  final Color? titleColor;

  /// 左边按钮文字颜色
  final Color? leftBtnColor;

  /// 右边按钮文字颜色
  final Color? rightBtnColor;

  /// 标题文字大小
  final double? titleFontSize;

  /// 按钮文字
  final double? btnFontSize;

  //只显示左侧
  final bool isShowLeft;

  //只显示右侧
  final bool isShowRight;

  // 上边距
  final double? customTopPadding;

  final double? customTitleBottomPadding;

  // 下边距
  final double? customBottomPadding;

  const KqCustomDialog({
    Key? key,
    this.titleString,
    required this.customWidgetBuilder,
    this.customBtnWidget,
    this.leftBtnText,
    this.width,
    this.rightBtnText,
    this.titleColor,
    this.leftBtnColor,
    this.rightBtnColor,
    this.titleFontSize,
    this.btnFontSize,
    this.onLeftBtnTap,
    this.onRightBtnTap,
    this.bgColor,
    this.customTopPadding,
    this.customTitleBottomPadding,
    this.customBottomPadding,
    this.isShowLeft = false,
    this.isShowRight = false,
    super.elevation,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // todo 平板布局写死横屏，竖屏要重写布局，用不到pad的组件库，所以不用判断横竖屏
    // bool isLangScape = context.orientation == Orientation.landscape;

    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1,
      height: customTopPadding ?? 36,
    ));

    /// 标题
    if (titleString != null) {
      children.add(Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
              fontSize: 16,
              color: KqThemeManager.getDialogConfig().titleColor ??
                  KqThemeColors.text26,
              fontWeight: FontWeight.w600),
        ),
      ));
      children.add(SizedBox(
        width: 1,
        height: customTitleBottomPadding ?? 24,
      ));
    }

    //自定义内容
    children.add(customWidgetBuilder());

    /// 下边距
    children.add(SizedBox(
      width: 1,
      height: customBottomPadding ?? 38,
    ));

    /// 底部按钮
    if (isShowLeft || isShowRight) {
      children.add(const KqDivider(
        height: 1,
      ));
    }

    children.add(customBtnWidget ??
        Row(
          children: [
            //不设置左右 都展示
            if (isShowLeft)
              Expanded(
                  child: KqInkWell(
                onTap: () {
                  if (onLeftBtnTap != null) {
                    onLeftBtnTap!();
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  height: 44,
                  child: Text(
                    leftBtnText ?? KqIntl.currentResource.cancel,
                    style: TextStyle(
                        fontSize: 16,
                        color: leftBtnColor ??
                            KqThemeManager.instance
                                .getConfig()
                                .dialogConfig
                                .cancelBtnColor ??
                            KqThemeColors.text8C),
                  ),
                ),
              )),
            if (isShowLeft && isShowRight)
              const KqVerticalDivider(
                height: 44,
              ),
            //不需要强制
            //需要强制  并且左边的没传数据
            if (isShowRight)
              Expanded(
                  child: KqInkWell(
                onTap: () {
                  if (onRightBtnTap != null) {
                    onRightBtnTap!();
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  height: 44,
                  child: Text(
                    rightBtnText ?? KqIntl.currentResource.confirm,
                    style: TextStyle(
                        fontSize: 16,
                        color: rightBtnColor ??
                            KqThemeManager.instance
                                .getConfig()
                                .dialogConfig
                                .mainBtnColor ??
                            KqThemeColors.textLightBlue),
                  ),
                ),
              ))
          ],
        ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Container(
            padding: EdgeInsets.only(bottom: padding),
            width: width ?? 320,
            child: Material(
                elevation: elevation ?? 0,
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(16))),
                color: bgColor ??
                    KqThemeManager.getDialogConfig().bgColor ??
                    KqThemeColors.bgWhite,
                child: ClipRRect(
                  borderRadius: const BorderRadius.all(Radius.circular(16)),
                  child: dialogChild,
                ))));
  }
}
